
// draws the login box 

    var modLogin = new Class({
    	    Extends:boxWindow,
			options: {
				width: 200,
				height: 250,
				BtnRefresh: false,
				BtnMin: false,
				BtnSettings: false,
				title: lang.label('auth_lnk_sign_in'),
				name: 'modLogin',
				uniqueCss: false,
				closeconfirm: false,
				modal: true,
				dbHandler: "talker.php"
			},					  
			
			
			/// draw the box or just check the user status
			initialize: function(initial){
				
				
				if(initial == null){
					this.parent(this.options); 
					var content = '<div  style="padding: 8px; width: 90%;"><div>'+lang.label('inp_email'); 
					content += '</div><div><input value=""  name="email"  id="email" size="30" type="text"></div>';
					content += '<div>'+lang.label('inp_pwd')+'</div>';	
					content += '<div><input  name="password" id="password" size="30" type="password" value=""></div>';	
					content += '<div style="text-align: center; margin-top: 12px;">';
					content += '<input id="btn_login" value="'+lang.label('auth_load_my_page')+'" onclick="ath.login();" type="button"></div>';
					
					content += '<h1>'+lang.label('auth_lnk_forget_pwd')+'</h1><div>'+lang.label('inp_email'); 
					content += '</div><div><input value=""  name="email_forgot"  id="email_forgot" size="30" type="text"></div>';
					content += '<div style="text-align: center; margin-top: 12px;">';
					content += '<input id="btn_forgot"  value="'+lang.label('btn_send')+'" onclick="ath.forgot();" type="button"></div>';
					
					content += '</div>';
					
					this.fillTitle(this.options.title);
					this.fillContent(content);
					
					//increase the height of the grayed sreen
				    $('overlay').setStyle('height', getFullHeight() +'px'); 		

					//focus
					$('email').select();
					
				}else{
					this.checkStatus();
				}
			},
			
			/// get info from db
			checkStatus: function(){
				var uhash = Cookie.get('un',{path: wf_cookie_path});

				if(uhash == ""){
					checkAuth('F');
				} else if( server_check_result == "" ) {
					var pid=1;

					lastTab = Cookie.get("lastTab",{path: wf_cookie_path});
					if(lastTab) pid=lastTab;
					this.sendData("cid="+wfcid+"&act=check&hash="+uhash+"&pid="+pid, this.checkResponse);
                                } else {
					this.checkResponse(server_check_result);
				}
			},


			// act on the info from db
			checkResponse: function(request)
			{
				var resultArray = Array();
				resultArray = JSON.decode(request);
				if (resultArray.response == "OK")
				{ 	
					//widgets  = spl[1];
					widgets = "";
					resultArray.result.udata.each(function(udata)
					{
						widgets += 	udata.modname + ':' + 
									udata.column + ':' + 
									udata.wid + ':' + 
									udata.showMin + ':' + 
									udata.showClose + ':' + 
									udata.showSetting + ':' + 
									udata.showHelp + ':' +
									udata.allowDrag + ':' + 
									udata.UniqueCss + ':' + 
									udata.page + ':' + 
									udata.order + "|"; 
					});
					
					//tabs     = spl[2];
					tabs = "";
					resultArray.result.pdata.each(function(pdata)
					{
						tabs += pdata.sqeuenceNumber + "<::>" +
								pdata.pageName + "<::>" +
								pdata.layout + "<::>" +
								pdata.icon + "<::>" +
								pdata.allowConfig + "<::>" +
								pdata.allowClose + "<::>" +
								pdata.fixedColumn + "<::>" +
								pdata.eventCode + "<::>" +
                                pdata.pageNumber + "<::>" +
                                pdata.boxWidth + "<::>" +
                                pdata.autoFit + "<::>" +
                                pdata.isSlideShow + "<::>" +
								pdata.intIntervalSlideShow + "<||>";
					});
					
					//old cookie.
					var ol = Cookie.get("LANG",{path: wf_cookie_path});
					//alert('old '+ ol +' frm db : '+ spl[3]);
					
					if(! ol)	//if someone has alraedy clicked on lang before sign in.. set that as default.
						Cookie.set("LANG", resultArray.result.lang, {path: wf_cookie_path});
					else
						Cookie.set("LANG", ol, {path: wf_cookie_path});						
					
					//alert( 'new '+ Cookie.get('LANG') );
					
					Cookie.set("theme", resultArray.result.theme, {path: wf_cookie_path});
					Cookie.set("lastID", resultArray.result.lastID, {path: wf_cookie_path}); 
				 
					checkAuth('V'); 
					//setTimeout(" createFirstTab("+ lastTab +"); ", 500 );			
				}
				else
				{
					checkAuth('F');	
				}
			},
			
			/// send login data to db
			login: function(){
				$('btn_login').setProperty('disabled' , true);
				
				var email = $('email').getProperty('value');
				var passw = $('password').getProperty('value');
				
				if(email.trim() != "" && passw.trim() != "" ){ 
					lastTab = Cookie.get("lastTab",{path: wf_cookie_path});
					if(!lastTab) lastTab=1;
					
					this.sendData("act=login&email=" + email + "&password=" + passw+"&pid="+lastTab, this.loginResponse);	
				}else{
					alert(lang.label('auth_input_invalid'));	
					$('btn_login').setProperty('disabled' , false);
				}
			},
			
			//// act on response for login request
			loginResponse: function(request){
				var resultArray = Array();
				resultArray = JSON.decode(request);

				if (resultArray.response == "ERR") {
					alert(lang.label('auth_login_error'));
					$('btn_login').setProperty('disabled' , false);
				} else { 
					Cookie.set('un', resultArray.result, {path: wf_cookie_path});
					refreshPage();
				}
			},
			
			
			///// send forgot pass info to db
			forgot: function(){
				$('btn_forgot').setProperty('disabled' , true);
				
				var email = $('email_forgot').getProperty('value');
				
				if(email.trim() != "" ){
					this.sendData("act=forgot&email=" + email, this.forgotResponse);	
				}else{
					alert(lang.label('auth_input_invalid'));	
					$('btn_forgot').setProperty('disabled' , false);
				}
			},
			
			/// act on response from backend for forgot password
			forgotResponse: function(request){
				var resultArray = Array();
				resultArray = JSON.decode(request);

				if(resultArray.response == "ERR"){
					alert(lang.label('auth_msg_email_invalid'));
					$('btn_forgot').setProperty('disabled' , false);
				}else if(resultArray.response == "OK"){
					alert(lang.label('auth_input_forgot_sent'));
					$('btn_forgot').setProperty('disabled' , false);
				}
			}
	});
